package zhangcy.day0109.carlottery.service;

import org.apache.log4j.Logger;
import zhangcy.day0109.carlottery.constants.UserStatus;
import zhangcy.day0109.carlottery.dao.UserDao;
import zhangcy.day0109.carlottery.po.User;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 *  汽车摇号业务数据处理
 */
public class UserMgr {
    // 定义Logger日志实例
    private Logger logger = Logger.getLogger(UserMgr.class);
    /**
     * 显示所有摇号用户列表信息
     */
    public void showUser(UserDao userDao, Scanner sc){
//        Object<String, String> mapUser = queryUser(userDao);
        System.out.println("根据中签编号模糊查询");
        System.out.print("请输入部分中签编号(不输入默认查询所有)：");
        String markNo = sc.nextLine();
        Map<String, Object> map = new HashMap<>();
        if (markNo != null && !"".equals(markNo)){
            map.put("markNo", markNo);
        }
//        获取查询到的摇号用户信息
        System.out.println("显示用户信息");
        List<User> userList = userDao.queryUserBy(map);
        System.out.println("用户姓名\t中签编号\t持照状态\t手机号码\t摇号日期");
        for (User u : userList){
            System.out.println(u.getUserName() + "\t" + u.getMarkNo() + "\t" +
                    UserStatus.getUserStatusByCode(u.getStatus()).getValue() + "\t" +
                    u.getPhone() + "\t" + u.getOpenDate());
        }
    }
    public static String getNumSmallCharRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出小写字母
                val += (char)(random.nextInt(26) + 97);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                //输出数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
    public void saveUserInfo(UserDao userDao, Scanner sc){
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        User user = new User();
        System.out.println("添加摇号用户");
        System.out.print("请输入用户姓名：");
        user.setUserName(sc.nextLine());
        user.setMarkNo(getNumSmallCharRandom(10));
        System.out.print("请输入手机号：");
        user.setPhone(sc.nextLine());
        user.setOpenDate(date.format(formatter));
        int addRes = userDao.insertUser(user);
        System.out.println(addRes > 0 ? "录入成功" : "录入失败");
    }
    public void lottery(UserDao userDao) {
        Map<String, Object> map = new HashMap<>();
        List lottery = new ArrayList<>();
        List lotterys = new ArrayList();
        List<User> usersLists = userDao.queryUserBy(map);
        for (User u:usersLists) {
            lottery.add(u.getMarkNo());
        }
        System.out.println("请输入需要摇取中签的数量:");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int len = lottery.size();
        System.out.println("摇取中签"+number+"个号码为：");

        do {
            int num = 0;
            Object str = lottery.get((int) Math.round(Math.random()*(len - 1)));
            for (int i = 0;i<lotterys.size();i++){
                if (str.equals(lotterys.get(i))){
                    continue;
                }else{
                    num++;
                }
            }
            if (num == lotterys.size()){
                lotterys.add(str);
                System.out.println(str);
            }
        }while (lotterys.size()<number);
    }
}
